package com.zy.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zy.yygh.hosp.repository.DepartmentRepository;
import com.zy.yygh.hosp.service.DepartmentService;
import com.zy.yygh.model.hosp.Department;
import com.zy.yygh.vo.hosp.DepartmentQueryVo;
import com.zy.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


    //    添加科室信息
    @Override
    public void save(Map<String, Object> paramMap) {

//        把map集合转成对象的方法
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department= JSONObject.parseObject(paramMapString,Department.class);
//        根据医院编号和科室信息查询是否存在
        Department departmentExit =
                departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if (departmentExit!=null){      // 如果存在
            departmentExit.setUpdateTime(new Date());
            departmentExit.setIsDeleted(0);
            departmentRepository.save(departmentExit);
        }else {     // 如果不存在
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

//    科室查询
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
//        创建pageable对象，设置当前页和每页记录数
//        0是第一页
        Pageable pageable= PageRequest.of(page-1,limit);
//        创建Example对象
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher exampleMatcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example=Example.of(department,exampleMatcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

//    删除科室接口
    @Override
    public void remove(String hoscode, String depcode) {
        //  根据医院和科室编号查询是否存在
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
        //调用方法删除
            departmentRepository.deleteById(department.getId());
        }

    }

//    查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDepTree(String hoscode) {
        //创建list试下按最终封装
        List<DepartmentVo> result= new ArrayList<>();
        //根据医院编号查询所有科室信息
        Department departmentQuery =new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表信息 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号 bigcode 进行分组，获取下列子科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历departmentMap
        for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
            //得到大科室编号
            String bigCode = entry.getKey();
            //大大科室编号对应的所有的全局数据
            List<Department> department1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1=new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(department1List.get(0).getBigname());
            //封装小科室
            List<DepartmentVo>children=new ArrayList<>();
            for (Department department: department1List){
                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室集合放到大科室的children里面去
            departmentVo1.setChildren(children);
            //放到最终的result集合中去
            result.add(departmentVo1);
        }
        //返回
        return result;
    }

//   根据科室编号和医院编号查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

//       根据科室编号和医院编号查询科室名称
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

}
